package gateway.config;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.config.GatewayReactiveLoadBalancerClientAutoConfiguration;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: MyLoadBalancerClientConfiguration
 * @Description:
 * @Author guochuang
 * @Date 2024/7/31
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties(LoadBalancerProperties.class)
@AutoConfigureBefore(GatewayReactiveLoadBalancerClientAutoConfiguration.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class MyLoadBalancerClientConfiguration {

    @Bean
    public MyLoadBalancer getMyLoadBalancer(DiscoveryClient discoveryClient){
        return new MyLoadBalancer(discoveryClient);
    }

    @Bean
    public ReactiveLoadBalancerClientFilter gatewayLoadBalancerClientFilter(DiscoveryClient discoveryClient,MyLoadBalancer myLoadBalancer, LoadBalancerProperties properties){
        return new CustomLoadBalancerClientFilter(properties, myLoadBalancer, discoveryClient);
    }
}